extern "C"
{
#include <config.h>
#include <epan/epan.h>
#include <epan/epan_dissect.h>
#include <register.h>
#include <epan/packet.h>
#include <epan/emem.h>
}
#include "Parser.h"
#include "Tree.h"
#include "Data.h"
#include "EpanWrap.h"

Parser::Parser()
{
	/* initialize memory allocation subsystem */
//	ep_init_chunk();
//	se_init_chunk();

	epan_init(register_all_protocols, 
			  register_all_protocol_handoffs, 
			  0, 
			  0, 
			  ReportFailure, 
			  ReportOpenFailure, 
			  ReportReadFailure
			  );
	init_dissection();
}

Parser::~Parser()
{
	cleanup_dissection();
	epan_cleanup();
}

Tree * Parser::Parse(const Data & message)
{
	class EDT
	{
	public:
		EDT(epan_dissect_t * _edt) {edt = _edt;};
		~EDT() {epan_dissect_free(edt);};
		epan_dissect_t * get() {return edt;};
	private:
		epan_dissect_t * edt;

	};
	EDT edt(epan_dissect_new(true, true));
	epan_dissect_run2(edt.get(), message.message, message.protocol);
	Tree * result = new Tree(*(edt.get()->tree));
	return result;
}

